﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Common;

using AppOne.db;
using AppOne.data;

namespace AppOne
{
    public partial class _Report01 : System.Web.UI.Page
    {

        private int number_of_one_page = 100;

        protected void Page_Load(object sender, EventArgs e)
        {
            //本页面的初始化工作，只有第一次显示的时候，做一次
            if (!IsPostBack)
            {

                //开始和结束时间
                string datetimeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                DateTimeStart.Text = "2001-07-01 08:08";
                DateTimeEnd.Text = datetimeNow;

                //名称（地点）选择列表
                ShowNameList();
            }

            //每页显示的行数
            number_of_one_page = XConvertToInt(TextBoxNumberOfRowsOnePage.Text, 100);
            //设置Table表格的头部标题
            //取得Url传过来的参数
            string stype = Request.QueryString["stype"];
            SetTableHeader(stype);
        }

        //显示dropdownList选项
        private void ShowNameList()
        {
            //清除原来的
            DropDownListName.Items.Clear();

            //获得所有名字，去除重复
            List<string> nameList = DB.QueryAllName();
            ListItem item;
            foreach (string str in nameList)
            {
                item = new ListItem();
                item.Value = str;
                item.Text = str;
                DropDownListName.Items.Add(item);
            }
            DropDownListName.SelectedValue = DropDownListName.Text;
        }

        //查询并设置表格数据
        private void ShowData(string datetimeStart, string datetimeEnd, string name, int page, string stype=null)
        {
            //查询结果
            List<jlrd> list = DB.Query(datetimeStart, datetimeEnd, name, number_of_one_page, page);

            #region 声明构造表格的变量
            TableRow row;

            TableCell cell;
            #endregion

            #region 遍例所有结果，把所有结果显示在网页上
            for (int i = 0; i < list.Count; ++i)
            {
                //产生一个新行
                row = new TableRow();

                cell = new TableCell();
                cell.Text = (i + 1).ToString();                
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = list[i].Lg_numeric.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = list[i].Type_int.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = list[i].Id_int.ToString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = list[i].Dt_date.ToShortDateString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = list[i].Tm_time.ToShortTimeString();
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = list[i].Name_char.ToString();
                row.Cells.Add(cell);

                //显示数据
                cell = new TableCell();
                switch (stype)
                {
                    case "tt": cell.Text = list[i].Tt_float.ToString(); break;
                    case "pp": cell.Text = list[i].Pp_float.ToString(); break;
                    case "fl": cell.Text = list[i].Fl_float.ToString(); break;
                    case "df": cell.Text = list[i].Df_float.ToString(); break;
                    case "dh": cell.Text = list[i].Dh_float.ToString(); break;
                    case "mf": cell.Text = list[i].Mf_float.ToString(); break;
                    case "mh": cell.Text = list[i].Mh_float.ToString(); break;
                    default: cell.Text = "tt"; break;
                }
                row.Cells.Add(cell);

                Table1.Rows.Add(row);
            }
            #endregion
        }

        //设置表格头
        private void SetTableHeader(string stype = null)
        {
            #region 声明构造表格的变量
            TableRow row;
            TableCell cell = null;
            #endregion

            #region 产生表头
            //产生一个新行
            row = new TableRow();

            cell = new TableCell();
            cell.Text = "No.";
            cell.Width = 40;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "lg";
            cell.Width = 100;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "Type";
            cell.Width = 100;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "id";
            cell.Width = 100;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Width = 100;
            cell.Text = "dt";
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "tm";
            cell.Width = 100;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "name";
            cell.Width = 150;
            row.Cells.Add(cell);


            #region 显示的数据(根据点击的按钮，显示数据)
            cell = new TableCell();
            cell.Width = 300;
            switch (stype)
            {
                case "tt": cell.Text = "tt"; break;
                case "pp": cell.Text = "pp"; break;
                case "fl": cell.Text = "fl"; break;
                case "df": cell.Text = "df"; break;
                case "dh": cell.Text = "dh"; break;
                case "mf": cell.Text = "mf"; break;
                case "mh": cell.Text = "mh"; break;
                default  : cell.Text = "tt"; break;
            }
            row.Cells.Add(cell);
            #endregion

            row.Font.Bold = true;
            row.BorderStyle = BorderStyle.Double;
            row.BorderColor = System.Drawing.Color.Green;
            Table1.Rows.Add(row);
            #endregion
        }

        //确定按钮
        protected void BtnFlush_Click(object sender, EventArgs e)
        {
            //取得Url传过来的参数
            string stype = Request.QueryString["stype"];
            if (string.IsNullOrEmpty(stype))
            {
                //发生错误
            }

            //获得两个时间，和名字 作为查询条件
            string timeStart = DateTimeStart.Text;
            string timeEnd = DateTimeEnd.Text;
            string name = DropDownListName.Text;

            //总页数
            int nTotalPage = GetPages(timeStart, timeEnd, name);

            int nCurrentPage = 1;
            ShowData(timeStart, timeEnd, name, nCurrentPage, stype);

            //设置按钮状态
            SettBtnStates(nTotalPage, nCurrentPage);
        }

        //跳到第一页
        protected void LinkBtnFirst_Click(object sender, EventArgs e)
        {
            //取得Url传过来的参数
            string stype = Request.QueryString["stype"];
            if (string.IsNullOrEmpty(stype))
            {
                //发生错误
            }

            //获得两个时间，和名字 作为查询条件
            string timeStart = DateTimeStart.Text;
            string timeEnd = DateTimeEnd.Text;
            string name = DropDownListName.Text;

            //总页数
            int nTotalPage = GetPages(timeStart, timeEnd, name);

            int nCurrentPage = 1;
            ShowData(timeStart, timeEnd, name, nCurrentPage, stype);

            //设置按钮状态
            SettBtnStates(nTotalPage, nCurrentPage);
        }

        //跳到最后一页
        protected void LinkBtnLast_Click(object sender, EventArgs e)
        {
            //取得Url传过来的参数
            string stype = Request.QueryString["stype"];
            if (string.IsNullOrEmpty(stype))
            {
                //发生错误
            }

            //获得两个时间，和名字 作为查询条件
            string timeStart = DateTimeStart.Text;
            string timeEnd = DateTimeEnd.Text;
            string name = DropDownListName.Text;

            //总页数
            int nTotalPage = GetPages(timeStart, timeEnd, name);

            int nCurrentPage = nTotalPage;
            ShowData(timeStart, timeEnd, name, nCurrentPage, stype);

            //设置按钮状态
            SettBtnStates(nTotalPage, nCurrentPage);
        }

        //上一页
        protected void LinkBtnPre_Click(object sender, EventArgs e)
        {
            //取得Url传过来的参数
            string stype = Request.QueryString["stype"];
            if (string.IsNullOrEmpty(stype))
            {
                //发生错误
            }

            //获得两个时间，和名字 作为查询条件
            string timeStart = DateTimeStart.Text;
            string timeEnd = DateTimeEnd.Text;
            string name = DropDownListName.Text;

            //总页数
            int nTotalPage = GetPages(timeStart, timeEnd, name);

            string strCurrentPage = HiddenFieldCurrentPage.Value;
            int nCurrentPage = XConvertToInt(strCurrentPage, 1);
            int nPrePage = 1;
            if (nCurrentPage > 1) //大于第一页，显示前一页
            {
                nPrePage = nCurrentPage - 1;
            }
            else                //没有前一页，显示第一页
            {
                nPrePage = 1;
            }
            ShowData(timeStart, timeEnd, name, nPrePage, stype);

            //设置按钮状态
            SettBtnStates(nTotalPage, nPrePage);
        }

        //下一页
        protected void LinkButNext_Click(object sender, EventArgs e)
        {
            //取得Url传过来的参数
            string stype = Request.QueryString["stype"];
            if (string.IsNullOrEmpty(stype))
            {
                //发生错误
            }

            //获得两个时间，和名字 作为查询条件
            string timeStart = DateTimeStart.Text;
            string timeEnd = DateTimeEnd.Text;
            string name = DropDownListName.Text;

            //总页数
            int nTotalPage = GetPages(timeStart, timeEnd, name);

            string strCurrentPage = HiddenFieldCurrentPage.Value;
            int nCurrentPage = XConvertToInt(strCurrentPage, 1);
            int nPrePage = 1;
            if (nCurrentPage < nTotalPage) //小于总页数,说明有下一页, 显示后一页
            {
                nPrePage = nCurrentPage + 1;
            }
            else                //没有后一页，显示最后一页
            {
                nPrePage = nTotalPage;
            }
            ShowData(timeStart, timeEnd, name, nPrePage, stype);

            //设置按钮状态
            SettBtnStates(nTotalPage, nPrePage);
        }

        //获得总页数
        private int GetPages(string timeStart, string timeEnd, string name)
        {
            //一共多少页
            int nTotalRow = DB.QueryTotalRow(timeStart, timeEnd, name);
            //向上取整(eg.3/2=2)
            int nTotalPage = Convert.ToInt32(Math.Ceiling((double)nTotalRow / (double)number_of_one_page));

            return nTotalPage;
        }

        //跳向第几页（根据用户输入）
        protected void LinkBtnGoToPage_Click(object sender, EventArgs e)
        {
            //取得Url传过来的参数
            string stype = Request.QueryString["stype"];
            if (string.IsNullOrEmpty(stype))
            {
                //发生错误
            }

            //获得两个时间，和名字 作为查询条件
            string timeStart = DateTimeStart.Text;
            string timeEnd = DateTimeEnd.Text;
            string name = DropDownListName.Text;

            //总页数
            int nTotalPage = GetPages(timeStart, timeEnd, name);

            string strGotoPage = string.IsNullOrEmpty(TextBoxGoToPage.Text) ? "1" : TextBoxGoToPage.Text;
            int nGotoPage = XConvertToInt(strGotoPage, 1);
            int nCurrentPage = 1;
            if (nGotoPage >= 1 && nGotoPage <= nTotalPage) //在正常页数范围之内
            {
                nCurrentPage = nGotoPage;
            }
            else if (nGotoPage <= 0) //显示第一页
            {
                nCurrentPage = 1;
            }
            else if (nGotoPage > nTotalPage)//显示最后一页
            {
                nCurrentPage = nTotalPage;
            }
            ShowData(timeStart, timeEnd, name, nCurrentPage, stype);

            //设置按钮状态
            SettBtnStates(nTotalPage, nCurrentPage);
        }

        //根据总页数和当前显示的页数，设置首页，末页，下一页，上一页四个链接的状态
        private bool SettBtnStates(int nTotalPage, int nCurrentPage)
        {
            //如果总页数为0(或1),全部不显示
            if (1 >= nTotalPage)
            {
                LinkBtnFirst.Enabled = false;
                LinkBtnPre.Enabled = false;
                LinkButNext.Enabled = false;
                LinkBtnLast.Enabled = false;
            }
            else    //如果有多页,根据当前显示的页，设置四个按钮的状态
            {
                if (nTotalPage == nCurrentPage)//如果显示页为最后一页，则 末页，下一页 不可点击 
                {
                    LinkBtnFirst.Enabled = true;
                    LinkBtnPre.Enabled = true;
                    LinkButNext.Enabled = false;
                    LinkBtnLast.Enabled = false;
                }
                else if (1 == nCurrentPage)//如果当前显示的页为第一页，则 首页，上一页 不可点
                {
                    LinkBtnFirst.Enabled = false;
                    LinkBtnPre.Enabled = false;
                    LinkButNext.Enabled = true;
                    LinkBtnLast.Enabled = true;
                }
                else//不是第一页，也不是最后一页，四个按钮全部显示
                {
                    LinkBtnFirst.Enabled = true;
                    LinkBtnPre.Enabled = true;
                    LinkButNext.Enabled = true;
                    LinkBtnLast.Enabled = true;
                }
            }




            //记当前是第几页
            HiddenFieldCurrentPage.Value = nCurrentPage.ToString();

            //显示(第1/10页)
            LabelPage.Text = nCurrentPage.ToString() + "/" + nTotalPage;

            TextBoxGoToPage.Text = nCurrentPage.ToString();
            return true;
        }

        //把一个文本转换个整型返回，如果出异常,返回 ndefault
        private int XConvertToInt(string text, int ndefault = 0)
        {
            int nret = ndefault;
            try
            {
                nret = Convert.ToInt32(text);
            }
            catch (System.Exception ex)
            {
                nret = ndefault;
            }
            return nret;
        }
    }
}